;; $Id: oaxaca.scm 405 2014-03-06 04:33:08Z alan.watson.f $

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(unless (and (finder-initialized?) (finder-idle?))
  (log-info "waiting for finder.")
  (finder-wait))
(log-info "finder ready.")

(unless (housekeeping-initialized?)
  (log-info "waiting for housekeeping.")
  (housekeeping-wait))
(log-info "housekeeping ready.")

(unless (and (instrument-initialized?) (instrument-idle?))
  (log-info "waiting for instrument.")
  (instrument-wait))
(log-info "instrument ready.")

(unless (and (lamp-initialized?) (lamp-idle?))
  (log-info "waiting for lamp.")
  (lamp-wait))
(log-info "lamp ready.")

(unless (and (wfc-initialized?) (wfc-idle?))
  (log-info "waiting for wfc.")
  (wfc-wait))
(log-info "wfc ready.")

(unless (and (wfs-initialized?) (wfs-idle?))
  (log-info "waiting for wfs.")
  (wfs-wait))
(log-info "wfs ready.")

(log-info "")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(log-info "configuring finder.")
(log-info "")

(finder-set-window-center! 708 522)
(finder-configure! 'full-frame-1)

;; Determined 2014-02-19 UTC.
(finder-set-r-vector! #(707.718529950869 521.769452639923))

;; Scale determined 2013-10-29.
(let ((scale 0.239))
  (finder-set-search-radius!     (exact (round (/ 30.0 scale))))
  (finder-set-refinement-radius! (exact (round (/ 5.0 scale))))
  (finder-set-c-matrix!
   (vector (vector scale 0.0)
           (vector 0.0 scale)
           #(0 0)
           #(0 0)
           #(0 0)
           #(0 0)
           #(0 0))))

(finder-set-g-vector! #(0.5 0.5 0 0 0 0 0))

(log-info "finished configuring finder.")
(log-info "")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(log-info "configuring instrument.")
(log-info "")

(instrument-set-window-center! 544 200)
(instrument-configure! 'full-frame-1)

;; Determined 2013-10-29.
(let ((scale 0.0302))
  (instrument-set-search-radius!     (exact (round (/ 10.0 scale))))
  (instrument-set-refinement-radius! (exact (round (/ 2.0 scale)))))

;; Determined 2014-02-19 UTC.
(instrument-set-r-vector! #(544.562898432967 199.169845616811))

;; Determined 2014-02-19 UTC.
(instrument-set-i-matrix!
 #(
   #(-0.555555555555555 -137.222222222222)
   #(-126.025256257331 10.898689964446)
   #(0 0)
   #(0 0)
   #(0 0)
   #(0 0)
   #(0 0)))

;; Determined 2014-02-19 UTC.
(instrument-set-c-matrix!
 #(
   #( -6.300e-04 -7.285e-03 )
   #( -7.932e-03 +3.211e-05 )
   #(0 0)
   #(0 0)
   #(0 0)
   #(0 0)
   #(0 0)))

(instrument-set-g-vector!
 #(0.25 0.25 0 0 0 0 0))

(log-info "finished configuring instrument.")
(log-info "")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(log-info "configuring wfs.")
(log-info "")

(wfs-set-window-center! 580 482)
(wfs-configure! 'full-frame-1)

(wfs-set-search-radius! 8)
(wfs-set-refinement-radius! 4)

(define (vector-shift dx dy old-v)
  (let* ((n (vector-length old-v))
         (new-v (make-vector n 0.0)))
    (do ((i 0 (+ i 2)))
        ((= i n) new-v)
      (vector-set! new-v (+ i 0) (+ (vector-ref old-v (+ i 0)) dx))
      (vector-set! new-v (+ i 1) (+ (vector-ref old-v (+ i 1)) dy)))))


;; For 5x5 square pupil.

;; Determined 2014-03-01 UTC.
(wfs-set-r-vector!
 (vector-shift -8 +16
               #(
554.207105263158 448.784360047847
554.505718674852 465.275571188952 
554.956707930894 482.031897700993 

570.460285085445 431.693746326961 
571.968597489145 498.153820694917 

587.197248284976 431.186900564272 
588.645533779402 497.898722430493 

603.922135527389 430.684939613665 
605.34775885339 497.381276330672 

620.989307978019 447.050783031777 
621.342027510425 463.761635605447 
621.690221674033 480.31265060929 

                 )))

;; For single-spot mode.
;;(wfs-set-window-center! 612 480)

;; Determined 2014-02-21 UTC.
(wfs-set-i-matrix!
 #(
#(-7.08319324952309 0.328080029994039 -6.77282196628522 -0.167820860545787 -7.01760354521971 -0.102643298038158 -7.21688634408939 0.358227259752763 -7.07279464363088 -0.127261878340753 -7.46322606945327 0.330490781727557 -7.14934241332934 0.0523656677531726 -7.28881297126309 0.224690421526144 -6.75435079097048 0.917102450626335 -7.13541092244703 0.271469952994331 -6.93424281198095 0.189148524707943 -7.52138506663357 0.338533124654887)
#(0.555050327523456 6.51844105109517 0.907845609353086 6.57498050490055 1.13239469910539 6.66120929717814 0.825231133193801 6.81932890008379 1.40495448890327 6.61001480642206 0.866054052520455 6.86765766904862 1.14307524343144 6.68610462347881 0.940397484932415 6.94967731297693 0.38802334000394 6.34171884354765 0.843595681240903 7.07014654409864 0.816984201954259 6.88468176628478 0.878138381197052 6.62832004730677)
#(-2.32273463865642 -1.46752656443017 -2.09879650749684 -0.210950339468927 -2.15409819815505 0.955590536269544 -1.20629542176331 -2.81746851422045 -0.769545222804824 2.08210594415248 0.153858002895196 -2.80458216074228 0.355247865829129 2.02332292528802 1.3608174464707 -2.84495408252634 1.32743398164919 1.6900403447558 2.65888146347428 -1.6161868105224 2.47346517915654 -0.337252118872647 2.81991066078248 0.739669308131607)
#(-0.681325817064552 -0.420002199667411 -0.224625704165583 -0.478904508958596 -0.0480284873137135 -0.729983155466974 -0.845421969461179 0.0422860828953178 0.367190829588798 -0.616925677962736 -0.754886562991178 0.398816918557942 0.478638804484248 -0.399078370872251 -0.540075615352404 0.694510572840491 0.510415449013916 0.0141096420743894 -0.061352411612524 0.790394074106177 0.185131669820922 0.693783750580954 0.46532287402597 0.481169433568082)
#(-0.373976440450742 0.70103093352022 -0.491172187753127 0.371303113752271 -0.634057876059578 0.0977677423619462 0.138051197270583 0.881278579352133 -0.437211494758192 -0.483673796451916 0.383447005140683 0.746305714526265 -0.258423673022834 -0.588585772320205 0.682952991469361 0.505170616447707 0.107822036157962 -0.621652104142988 0.794833257946518 0.0680259457082736 0.685317828095467 -0.228931667747823 0.481705604443871 -0.41650611797213)
#(0.122046176661404 0.528884008671374 -0.175520702030272 0.284610741339684 -0.37903463190431 -0.0230515216466721 0.52480769461873 0.12856772066014 -0.0038335962632077 -0.411090933865918 0.336414117789595 -0.358732615845787 0.17648296328827 -0.196544677882073 0.0440541914422788 -0.628117616854515 0.298556581527802 0.193618235811246 -0.487036195482015 -0.216383298965128 -0.419119543708162 0.154156215595067 -0.125941083085526 0.377907880223923)
#(0.504843522082462 -0.136789185077401 0.225313272184001 0.198368022921025 -0.0926370670999353 0.420818914204028 0.0102816957279986 -0.596867362350392 -0.353023917970532 0.112819149073644 -0.331521303621503 -0.348834347948554 -0.162247900348152 -0.251702934619331 -0.576551266202251 -0.0180092175512788 0.191227068280742 -0.302506642444626 -0.277389066799508 0.574483338307093 0.100846988021743 0.415587724733643 0.419475798608838 0.13299725776391)
   ))


;; Determined 2014-02-21 UTC.
(wfs-set-c-matrix!
 #(
  #( -1.250e-02 +3.073e-03 -1.287e-02 +2.588e-03 -1.375e-02 +3.770e-03 -1.108e-02 +2.159e-03 -1.427e-02 +3.311e-03 -1.033e-02 +1.435e-03 -1.409e-02 +2.603e-03 -9.022e-03 +2.390e-04 -1.262e-02 +2.232e-03 -8.624e-03 -7.504e-04 -9.389e-03 -1.049e-03 -1.179e-02 -3.317e-04 )
  #( -1.510e-03 +1.095e-02 -5.253e-04 +1.191e-02 +2.972e-04 +1.310e-02 +2.699e-05 +1.008e-02 +1.951e-03 +1.516e-02 +8.489e-04 +9.331e-03 +1.716e-03 +1.617e-02 +1.234e-03 +8.960e-03 -1.639e-04 +1.569e-02 +4.816e-04 +1.053e-02 -1.312e-04 +1.250e-02 +1.878e-04 +1.411e-02 )
  #( -3.622e-02 -1.085e-02 -3.220e-02 +1.966e-03 -3.257e-02 +1.325e-02 -1.842e-02 -2.655e-02 -1.034e-02 +2.590e-02 -7.224e-04 -2.847e-02 +4.729e-03 +2.814e-02 +1.499e-02 -3.050e-02 +1.690e-02 +2.783e-02 +2.971e-02 -1.448e-02 +2.756e-02 +3.170e-03 +3.319e-02 +1.758e-02 )
  #( -1.194e-01 -4.465e-02 -4.909e-02 -7.904e-02 -1.903e-02 -1.502e-01 -9.681e-02 +1.867e-02 +9.803e-02 -1.387e-01 -7.733e-02 +3.550e-02 +1.273e-01 -6.852e-02 -4.988e-02 +5.571e-02 +1.283e-01 +3.392e-02 -1.416e-02 +8.805e-02 +1.905e-02 +1.134e-01 +8.284e-02 +1.096e-01 )
  #( -3.923e-02 +1.062e-01 -8.744e-02 +7.140e-02 -1.408e-01 +3.964e-02 +2.025e-02 +9.244e-02 -1.230e-01 -8.853e-02 +3.567e-02 +7.812e-02 -7.445e-02 -1.324e-01 +6.591e-02 +5.449e-02 +2.035e-02 -1.379e-01 +9.911e-02 +2.837e-02 +1.073e-01 -2.751e-02 +9.630e-02 -7.421e-02 )
  #( -1.028e-02 +2.083e-01 -1.043e-01 +9.750e-02 -1.797e-01 -5.558e-02 +1.700e-01 +6.641e-02 +2.883e-02 -2.169e-01 +1.072e-01 -1.232e-01 +1.235e-01 -1.055e-01 +7.097e-03 -2.238e-01 +1.861e-01 +9.428e-02 -1.829e-01 -3.841e-02 -1.391e-01 +1.206e-01 +9.182e-03 +2.071e-01 )
  #( +2.202e-01 +1.283e-03 +7.486e-02 +1.197e-01 -7.684e-02 +2.027e-01 +4.880e-02 -1.939e-01 -1.963e-01 +2.374e-02 -8.452e-02 -1.102e-01 -1.104e-01 -1.490e-01 -1.769e-01 +2.011e-03 +6.317e-02 -1.909e-01 -6.124e-02 +2.113e-01 +8.134e-02 +1.204e-01 +1.874e-01 -8.779e-03 )
   ))

(wfs-set-g-vector!
 #(0.10 0.10 0.05 0.05 0.05 0.05 0.05))

(define (wfs-tune! refinement-radius g-vector)
  (wfs-set-refinement-radius! refinement-radius)
  (wfs-set-g-vector g-vector))

(wfs-configure! 'sub-frame-2)

(log-info "")
(log-info "finished configuring wfs.")
(log-info "")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (list-rms a)
  (sqrt (/ (apply + (map (lambda (x) (* x x)) a))
           (length a))))

(let* ((instrument-scale 0.0302)
       (wfs-scale
        (* instrument-scale (list-rms (list (/ (list-rms (vector->list (vector-ref (instrument-i-matrix) 0)))
                                               (list-rms (vector->list (vector-ref (wfs-i-matrix) 0))))
                                            (/ (list-rms (vector->list (vector-ref (instrument-i-matrix) 1)))
                                               (list-rms (vector->list (vector-ref (wfs-i-matrix) 1)))))))))
  (log-info "WFS scale is " wfs-scale))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(log-info "configuring wfc.")
(log-info "")

(wfc-flatten!)

(log-info "finished configuring wfc.")
(log-info "")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
